<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test cases without parameters</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Boost.Test">
<link rel="up" href="../test_cases.html" title="Test cases">
<link rel="prev" href="../test_cases.html" title="Test cases">
<link rel="next" href="test_case_generation.html" title="Data-driven test cases">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../test_cases.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_cases.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="test_case_generation.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_test.tests_organization.test_cases.test_organization_nullary"></a><a class="link" href="test_organization_nullary.html" title="Test cases without parameters">Test
        cases without parameters</a>
</h4></div></div></div>
<p>
          The most common scenario is that you want to write test case without any
          parameters. The <span class="emphasis"><em>Unit Test Framework</em></span> provides you with
          both automatic and manual registration APIs to declare such test case.
        </p>
<a name="ref_BOOST_AUTO_TEST_CASE"></a><h5>
<a name="boost_test.tests_organization.test_cases.test_organization_nullary.h0"></a>
          <span class="phrase"><a name="boost_test.tests_organization.test_cases.test_organization_nullary.automated_registration"></a></span><a class="link" href="test_organization_nullary.html#boost_test.tests_organization.test_cases.test_organization_nullary.automated_registration">Automated
          registration</a>
        </h5>
<p>
          To declare a test case without parameters, which is registered in place
          of implementation, employ the macro <a class="link" href="../../utf_reference/test_org_reference/test_org_boost_auto_test_case.html" title="BOOST_AUTO_TEST_CASE"><code class="computeroutput"><span class="identifier">BOOST_AUTO_TEST_CASE</span></code></a>.
        </p>
<pre class="programlisting"><a class="link" href="../../utf_reference/test_org_reference/test_org_boost_auto_test_case.html" title="BOOST_AUTO_TEST_CASE"><code class="computeroutput"><span class="identifier">BOOST_AUTO_TEST_CASE</span></code></a><span class="special">(</span><span class="identifier">test_case_name</span><span class="special">);</span>
</pre>
<p>
          This API is designed to closely mimic nullary free function declaration
          syntax. In comparison with free function all you need to do is to skip
          result type and brackets and wrap test case name into BOOST_AUTO_TEST_CASE:
        </p>
<h6>
<a name="boost_test.tests_organization.test_cases.test_organization_nullary.h1"></a>
          <span class="phrase"><a name="boost_test.tests_organization.test_cases.test_organization_nullary.example_descr"></a></span><a class="link" href="test_organization_nullary.html#boost_test.tests_organization.test_cases.test_organization_nullary.example_descr">Example:
          Nullary function based test case with automated registration</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">free_test_function</span> <span class="special">)</span>
<span class="comment">/* Compare with void free_test_function() */</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="keyword">true</span> <span class="comment">/* test assertion */</span> <span class="special">);</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>

<span class="special">***</span> <span class="identifier">No</span> <span class="identifier">errors</span> <span class="identifier">detected</span>
</pre>
                </td></tr></tbody>
</table></div>
<p>
          With this macro you don't need to implement any other registration steps.
          The macro creates and registers the test case with the name <code class="computeroutput"><span class="identifier">free_test_function</span></code> automatically.
        </p>
<a name="ref_BOOST_TEST_CASE"></a><h5>
<a name="boost_test.tests_organization.test_cases.test_organization_nullary.h2"></a>
          <span class="phrase"><a name="boost_test.tests_organization.test_cases.test_organization_nullary.manual_registration"></a></span><a class="link" href="test_organization_nullary.html#boost_test.tests_organization.test_cases.test_organization_nullary.manual_registration">Manual
          registration</a>
        </h5>
<p>
          The <span class="emphasis"><em>Unit Test Framework</em></span> allows to manually create
          test case without parameters based on nullary free functions, nullary function
          objects (including those created with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span></code>
          and nullary <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span></code> instances). To do this, employ
          the macro <a class="link" href="../../utf_reference/test_org_reference/test_org_boost_test_case.html" title="BOOST_TEST_CASE and BOOST_TEST_CASE_NAME"><code class="computeroutput"><span class="identifier">BOOST_TEST_CASE</span></code></a>:
        </p>
<pre class="programlisting"><span class="identifier">BOOST_TEST_CASE</span><span class="special">(</span><span class="identifier">test_function</span><span class="special">);</span>
</pre>
<p>
          <a class="link" href="../../utf_reference/test_org_reference/test_org_boost_test_case.html" title="BOOST_TEST_CASE and BOOST_TEST_CASE_NAME"><code class="computeroutput"><span class="identifier">BOOST_TEST_CASE</span></code></a> creates an instance
          of the class <code class="computeroutput"><a class="link" href="../../../boost/unit_test/test_case.html" title="Class test_case">boost::unit_test::test_case</a></code>
          and returns a pointer to the constructed instance. The test case name is
          deduced from the macro argument test_function. If you prefer to assign
          a different test case name, you have either to
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
              use the macro <a class="link" href="../../utf_reference/test_org_reference/test_org_boost_test_case.html" title="BOOST_TEST_CASE and BOOST_TEST_CASE_NAME"><code class="computeroutput"><span class="identifier">BOOST_TEST_CASE_NAME</span></code></a> instead
            </li>
<li class="listitem">
              or use the underlying <code class="computeroutput"><a class="link" href="../../../header/boost/test/tree/test_unit_hpp.html" title="Header &lt;boost/test/tree/test_unit.hpp&gt;">make_test_case</a></code> interface
              instead.
            </li>
</ul></div>
<p>
          To register a new test case, employ the method <code class="computeroutput"><a class="link" href="../../../boost/unit_test/test_suite.html#idm44970733617744-bb">test_suite::add</a></code>. Both test case creation
          and registration are performed in the <a class="link" href="../../adv_scenarios/test_module_init_overview.html" title="Test module's initialization">test
          module initialization function</a>.
        </p>
<p>
          Here is the simplest example of manually registered test case. A single
          test case is created and registered inside the test module initialization
          routine. Note that the free function name is passed by address to the macro
          <a class="link" href="../../utf_reference/test_org_reference/test_org_boost_test_case.html" title="BOOST_TEST_CASE and BOOST_TEST_CASE_NAME"><code class="computeroutput"><span class="identifier">BOOST_TEST_CASE</span></code></a>`.
        </p>
<a name="ref_bt_example01"></a><h6>
<a name="boost_test.tests_organization.test_cases.test_organization_nullary.h3"></a>
          <span class="phrase"><a name="boost_test.tests_organization.test_cases.test_organization_nullary.example_descr0"></a></span><a class="link" href="test_organization_nullary.html#boost_test.tests_organization.test_cases.test_organization_nullary.example_descr0">Example:
          Nullary free function manually registered</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unit_test</span><span class="special">;</span>

<span class="keyword">void</span> <span class="identifier">free_test_function</span><span class="special">()</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="keyword">true</span> <span class="comment">/* test assertion */</span> <span class="special">);</span>
<span class="special">}</span>

<span class="identifier">test_suite</span><span class="special">*</span> <span class="identifier">init_unit_test_suite</span><span class="special">(</span> <span class="keyword">int</span> <span class="comment">/*argc*/</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*</span> <span class="comment">/*argv*/</span><span class="special">[]</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">framework</span><span class="special">::</span><span class="identifier">master_test_suite</span><span class="special">().</span>
    <span class="identifier">add</span><span class="special">(</span> <span class="identifier">BOOST_TEST_CASE</span><span class="special">(</span> <span class="special">&amp;</span><span class="identifier">free_test_function</span> <span class="special">)</span> <span class="special">);</span>
  <span class="identifier">framework</span><span class="special">::</span><span class="identifier">master_test_suite</span><span class="special">().</span>
    <span class="identifier">add</span><span class="special">(</span> <span class="identifier">BOOST_TEST_CASE_NAME</span><span class="special">(</span> <span class="special">&amp;</span><span class="identifier">free_test_function</span><span class="special">,</span> <span class="string">"second-check-free-test-function"</span> <span class="special">)</span> <span class="special">);</span>
  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">unit_scope</span>
<span class="identifier">Running</span> <span class="number">2</span> <span class="identifier">test</span> <span class="identifier">cases</span><span class="special">...</span>
<span class="identifier">Entering</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"Master Test Suite"</span>
<span class="identifier">example</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">20</span><span class="special">:</span> <span class="identifier">Entering</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"free_test_function"</span>
<span class="identifier">example</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">20</span><span class="special">:</span> <span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"free_test_function"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">50u</span><span class="identifier">s</span>
<span class="identifier">example</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">22</span><span class="special">:</span> <span class="identifier">Entering</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"second-check-free-test-function"</span>
<span class="identifier">example</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">22</span><span class="special">:</span> <span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"second-check-free-test-function"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">32u</span><span class="identifier">s</span>
<span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"Master Test Suite"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">158u</span><span class="identifier">s</span>

<span class="special">***</span> <span class="identifier">No</span> <span class="identifier">errors</span> <span class="identifier">detected</span>
</pre>
                </td></tr></tbody>
</table></div>
<p>
          A test case can be implemented as a method of a class. In this case a pointer
          to the class instance has to be bound to the test method to create a test
          case. You can use the same instance of the class for multiple test cases.
          The <span class="emphasis"><em>Unit Test Framework</em></span> doesn't take an ownership
          of the class instance and you are required to manage the class instance
          lifetime yourself.
        </p>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
            The class instance can't be defined in the initialization function scope,
            since it becomes invalid as soon as the test execution exits it. It needs
            to be either defined statically/globally or managed using a shared pointer.
          </p></td></tr>
</table></div>
<h6>
<a name="boost_test.tests_organization.test_cases.test_organization_nullary.h4"></a>
          <span class="phrase"><a name="boost_test.tests_organization.test_cases.test_organization_nullary.example_descr1"></a></span><a class="link" href="test_organization_nullary.html#boost_test.tests_organization.test_cases.test_organization_nullary.example_descr1">Example:
          Nullary method of a class bound to shared class instance and manually registered</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">bind</span><span class="special">/</span><span class="identifier">bind</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unit_test</span><span class="special">;</span>

<span class="keyword">class</span> <span class="identifier">test_class</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
  <span class="keyword">void</span> <span class="identifier">test_method1</span><span class="special">()</span>
  <span class="special">{</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="keyword">true</span> <span class="comment">/* test assertion */</span> <span class="special">);</span>
  <span class="special">}</span>
  <span class="keyword">void</span> <span class="identifier">test_method2</span><span class="special">()</span>
  <span class="special">{</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="keyword">false</span> <span class="comment">/* test assertion */</span> <span class="special">);</span>
  <span class="special">}</span>
<span class="special">};</span>

<span class="identifier">test_suite</span><span class="special">*</span> <span class="identifier">init_unit_test_suite</span><span class="special">(</span> <span class="keyword">int</span> <span class="comment">/*argc*/</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*</span> <span class="comment">/*argv*/</span><span class="special">[]</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="identifier">test_class</span><span class="special">&gt;</span> <span class="identifier">tester</span><span class="special">(</span> <span class="keyword">new</span> <span class="identifier">test_class</span> <span class="special">);</span>

  <span class="identifier">framework</span><span class="special">::</span><span class="identifier">master_test_suite</span><span class="special">().</span>
    <span class="identifier">add</span><span class="special">(</span> <span class="identifier">BOOST_TEST_CASE</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span> <span class="special">&amp;</span><span class="identifier">test_class</span><span class="special">::</span><span class="identifier">test_method1</span><span class="special">,</span> <span class="identifier">tester</span> <span class="special">)));</span>
  <span class="identifier">framework</span><span class="special">::</span><span class="identifier">master_test_suite</span><span class="special">().</span>
    <span class="identifier">add</span><span class="special">(</span> <span class="identifier">BOOST_TEST_CASE</span><span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bind</span><span class="special">(</span> <span class="special">&amp;</span><span class="identifier">test_class</span><span class="special">::</span><span class="identifier">test_method2</span><span class="special">,</span> <span class="identifier">tester</span> <span class="special">)));</span>
  <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
<span class="identifier">Running</span> <span class="number">2</span> <span class="identifier">test</span> <span class="identifier">cases</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">22</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"boost::bind( &amp;test_class::test_method2, tester )"</span><span class="special">:</span> <span class="identifier">check</span> <span class="keyword">false</span> <span class="identifier">has</span> <span class="identifier">failed</span>

<span class="special">***</span> <span class="number">1</span> <span class="identifier">failure</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"Master Test Suite"</span>
</pre>
                </td></tr></tbody>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../test_cases.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_cases.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="test_case_generation.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
